<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!Doctype html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<title>商品管理</title>
	<%@include file="/common/header.jsp" %>
</head>

<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
	<div class="layui-header">
		<div class="layui-logo">商品管理</div>
		<%@include file="/common/top.jsp" %>

	</div>

	<%@include file="/common/user_left.jsp" %>



	<div class="layui-body">
		<form class="layui-form">
			<blockquote class="layui-elem-quote quoteBox">
				<form class="layui-form">
					<div class="layui-inline">
						<div class="layui-input-inline">
							<input type="text" class="layui-input searchVal" placeholder="请输入搜索的内容" />
						</div>
						<a class="layui-btn search_btn" data-type="reload">搜索</a>
					</div>
					<div class="layui-inline">
						<a class="layui-btn layui-btn-normal addCommodity_btn">添加商品</a>
					</div>
				</form>
			</blockquote>

			<table id="commodityList" lay-filter="commodityList"></table>
			<!--操作-->
			<script type="text/html" id="commodityListBar">
				<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
				<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
			</script>
		</form>
	</div>
</div>


<script>
    //JavaScript代码区域
    layui.use(['form','layer','laydate','table','laytpl'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table;

        //新闻列表
        var tableIns = table.render({
            elem: '#commodityList',
            url : '/commodity/list.json',
            method:"POST",
            cellMinWidth : 95,
            page : true,
            height : "full-105",
            limit : 20,
            limits : [10,15,20,25],
            id : "commodityListTable",
            cols : [[
                {field: 'comName', title: '商品名称',templet:function(data){
                    return '<span class="layui-blue">'+data.comName+'</span>';
                }},
                {field: 'comType', title: '商品类型', align:'center'},
                {field: 'comIntroduce', title: '商品描述', align:'center'},
                {field: 'username', title: '用户名', align:'center'},
                {field: 'phone', title: '联系方式', align:'center'},
                {field: 'comPrice', title: '商品价格', align:'center'},
                {field: 'cDate', title: '发布日期', align:'center',templet:function(data){
                    return timestampToTime(data.cDate);
                }},
                {title: '操作', templet:'#commodityListBar',align:"center"}
            ]]
        });

        $(".search_btn").on("click",function(){
                table.reload("commodityListTable",{
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                        key: $(".searchVal").val()  //搜索的关键字
                    }
                })
        });

        //添加文章
        function addCommodity(edit){
            var content="";
            if(edit){
                content="/add_commodity.html?id="+edit.id;
            }else{
                content="/add_commodity.html";
            }
            window.location.href=content;
        }
        $(".addCommodity_btn").click(function(){
            addCommodity();
        })

        //列表操作
        table.on('tool(commodityList)', function(obj){
            var layEvent = obj.event,
                data = obj.data;
            if(layEvent === 'edit'){ //编辑
                addCommodity(data);
            } else if(layEvent === 'del'){ //删除
                layer.confirm('确定删除此商品？',{icon:3, title:'提示信息'},function(index){
                    $.post("/commodity/del_commodity",{
                        id : data.id  //将需要删除的Id作为参数传入
                    },function(data){
                        if(data.status==200){
                            tableIns.reload();
                            layer.close(index);
                        }else {
                            layer.close(index);
                            layer.msg(data.msg, {icon: 16, time: 1000, shade: 0.8});
                        }
                    })
                });
            }
        });
    })
    function timestampToTime(timestamp) {
        var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        Y = date.getFullYear() + '-';
        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        D = date.getDate()<10?('0'+date.getDate()):(date.getDate())+ ' ';
        h = date.getHours()<10?('0'+date.getHours()):(date.getHours()) + ':';
        m = date.getMinutes()<10?('0'+date.getMinutes()):(date.getMinutes()) + ':';
        s = date.getSeconds()<10?('0'+date.getSeconds()):(date.getSeconds());
        return Y+M+D;
    }
</script>
</body>
</html>